package com.shine.customview.widget.viewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by M_genius on 2017/8/31 11:40.
 * 描述：
 */

public class MulitImageShowView extends ViewGroup {

    int width;
    int height;
    private View childView;
    private int childWidth;
    private int childHeight;
    private int left;
    private int top;

    public MulitImageShowView(Context context) {
        super(context);
    }

    public MulitImageShowView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MulitImageShowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        left = 0;
        top = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            childView = getChildAt(i);
            childWidth = childView.getMeasuredWidth();
            childHeight = childView.getMeasuredHeight();
            if (childCount == 1) {
                childView = getChildAt(0);
                left = 0;
                top = 0;
            } else {
                if (childCount == 2) {

                    left = left + i * (childWidth + 5);

                } else if (childCount == 4) {
                    if (i % 2 == 0) {
                        left = 0;
                        top = top + (i / 2) * (childHeight + 5);
                    } else {
                        left = left + (i % 2) * (childWidth + 5);
                    }
                } else {

                    if (i % 3 == 0) {
                        left = 0;
                        top = top + (i / 3) * (childHeight + 5);
                    } else {
                        left = left + (i % 3) * (childWidth + 5);
                    }
                }
            }
            childView.layout(left, top, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**
         * 获得此ViewGroup上级容器为其推荐的宽和高，以及计算模式
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);


        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int childCount = getChildCount();
        if (childCount == 1) {
            childView = getChildAt(0);
            width = childView.getMeasuredWidth();
            height = childView.getMeasuredHeight();

        } else {
            for (int i = 0; i < childCount; i++) {
                childView = getChildAt(i);
                childWidth = childView.getMeasuredWidth();
                childHeight = childView.getMeasuredHeight();

                if (childCount == 2) {
                    width = childWidth + i * (childWidth + 5);
                    height = childHeight;
                } else if (childCount == 4) {
                    if (i < 2) {
                        width = childWidth + i * (childWidth + 5);
                        height = childHeight;
                    } else if(i == 2){
                        height = height + childHeight + 5;
                    }
                } else {
                    if (i < 3) {
                        width = childWidth + i * (childWidth + 5);
                        height = childHeight;
                    } else if (i % 3 == 0) {
                        height = height + childHeight + 5;
                    }
                }
            }
        }

        setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth
                : width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight
                : height);

    }
}
